<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyPrayer as CompanyPrayerModel;
use app\common\model\CompanyGongdeng as CompanyGongdengModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Practice as PracticeModel;
use app\common\model\Company as CompanyModel;

/**
 * 寺院祈福香控制器
 * Class Company
 * @package app\home\controller
 */
class Companyprayer extends Base
{
    protected $moren = [
        'name' => '祈福香',
        'intro' => '弟子愿向菩萨诚心祈祷，愿以此福德，功德回向',
        'standard' =>array(
            [
                'name' => '平安香',
                'img' => array(
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/pax.jpg',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/paxA.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/paxB.jpg',
                ),
                'price' => '0'
            ],[
                'name' => '增财香',
                'img' => array(
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/zcx.jpg',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/zcxA.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/zcxB.jpg',
                ),
                'price' => '3'
            ],[
                'name' => '增福香',
                'img' => array(
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/zfx.jpg',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/zfxA.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/zfxB.jpg',
                ),
                'price' => '3'
            ],[
                'name' => '延寿香',
                'img' => array(
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/ysx.jpg',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/ysxA.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/ysxB.jpg',
                ),
                'price' => '6'
            ],[
                'name' => '消灾香',
                'img' => array(
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/xzx.jpg',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/xzxA.png',
                    'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E7%A5%88%E7%A6%8F%E9%A6%99/xzxB.jpg',
                ),
                'price' => '9'
            ]
        )
    ];
    /**
     * [getprice  获取价格]
     * @param
     * @author [C。]
     * @return json
     */
    public function getprice()
    {
        $param = input('param.');
        $model = new CompanyprayerModel;
        $cmodel = new CompanyModel;
        try{
            $data = $model->findData($param['id'],'comid');
            if(!empty($data)){
                $data['standard'] = unserialize($data['standard']);
            }else{
                $data = $this->moren;
            }
            $company = $cmodel->findData($param['id'],'id','mch_id,much_secret,wx_appid,wx_secret,is_son');
            if($company['is_son'] == 1){
                if(empty($company['mch_id']) || empty($company['wx_appid'])){
                    return admin_json(2, '暂未开通');
                }
            }else{
                if(empty($company['wx_appid']) || empty($company['wx_secret']) || empty($company['mch_id']) || empty($company['much_secret'])){
                    return admin_json(2, '暂未开通');
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [gethuixiang  祈福支付完回向]
     * @param
     * @author [C。]
     * @return json
     */
    public function gethuixiang()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $pmodel = new PracticeModel;
        try{
            $name = $model->findData($param['id'],'id','gongdeng,type');
            $practice = $pmodel->getRound(['type'=>3],'content');
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '发愿成功',['huixiang'=>$practice['content'],'name'=>$name['gongdeng']]);
    }
    /**
     * [record  记录]
     * @param
     * @author [C。]
     * @return json
     */
    public function record()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $user = $this->user;
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['c.mid'] = $user['id'];
            $map['c.comid'] = $param['id'];
            $map['c.status'] = 1;
            $map['c.type'] = 3;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,'c.gongdeng,c.total_price,c.content,c.create_time,c.bless');
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [donornum  捐款人数]
     * @param
     * @author [C。]
     * @return json
     */
    public function donornum()
    {
        $param = input('param.');
        $model = new CompanyPrayerModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'comid','donor_num');
            if(empty($data)){
                $data['donor_num'] = 0;
            }
            $data['really'] = $cmodel->getAllCount(['type'=>3,'status'=>1,'comid'=>$param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyPrayerModel;
        $param = input('param.');

        try{
            $gongdeng = $model->findData($param['comid'],'comid');
            if(empty($gongdeng)){
                $param['standard'] = serialize($param['standard']);
                $model->addData($param);
            }else{
                $param['standard'] = serialize($param['standard']);
                $model->editDataBy($param,['comid'=>$param['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
}